package com.infocoll.pachou.controller;

import com.infocoll.pachou.bean.User;
import com.infocoll.pachou.biz.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean;

@RestController
public class MainController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/dummysave")
    public String dummysave() {
        userService.save();
        return "dummy save ok";
    }
    AtomicBoolean flag = new AtomicBoolean(false);

    @RequestMapping(value = "/refresh")
    public String folder(@RequestParam(value="path") String path) {
        System.out.println("path = " + path);

        if(flag.get()==true){
            System.out.println("已经运行过了， 若需要运行， 请清理数据库，重启服务。");
            return "已经运行过了， 若需要运行， 请清理数据库，重启服务。";
        }
        new Thread(){

            @Override
            public void run() {

                super.run();
                flag.set(true);
                try {
                    File file = new File(path);

                    File[] files = file.listFiles();
                    for (File item: files) {

                        System.out.println("path::" + item.getName()+" == "+item.getAbsolutePath());
                        if("supp".equalsIgnoreCase(item.getName()) || "embed".equalsIgnoreCase(item.getName())|| "embed_bak".equalsIgnoreCase(item.getName()) ){
                            continue;
                        }
                        try {
                            System.out.println("start...");
                            userService.parse(item.getAbsolutePath(), item.getName() );
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }.start();

        return "success";

    }

    int i=0;
    // @RequestParam(value = "type", required = false, defaultValue = "1")
    @RequestMapping(value = "/search")
    public Object folder(@RequestParam(value="language" , required = false) String language, @RequestParam(value="xuhao", required = false) String xuhao, @RequestParam(value="name", required = false) String name) {
        User user = new User();
        i++;
        user.setUserage(""+i);
        if(name!=null){
            return userService.findByUserName(language,name).iterator();
        }
        if(xuhao!=null){
            Optional<User> opuser = userService.findByUser_langAndUser_idno(language,xuhao);
            System.out.println("opuser =11 " + opuser);
            if( opuser.isPresent() ){
                return opuser.get();
            } else {
                System.out.println("opuser 1111 = " + user);
                return user;
            }
        }
        System.out.println("user = " + user);
        return user;
    }
}
